package com.alibaba.citrus.cr.forecast.repository.impl;

import com.alibaba.citrus.cr.forecast.repository.CartonSpecsRepository;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crforecastsales.cartonspecs.dto.*;
import com.epoch.app.crforecastsales.cartonspecs.model.CartonSpecs;
import com.epoch.app.crforecastsales.cartonspecs.service.CartonSpecsService;
import com.epoch.app.crforecastsales.model.dto.CartonSepcsRequest;
import com.epoch.app.crforecastsales.model.dto.CartonSpecsLoadListRequest;
import com.epoch.app.crforecastsales.model.dto.CartonSpecsLoadListVO;
import com.epoch.app.crforecastsales.model.dto.CartonSpecsSDO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: qianlei
 * @create: 2021/11/11 22:53
 */
@Component
public class CartonSpecsRepositoryImpl implements CartonSpecsRepository {
    @Autowired
    CartonSpecsService cartonSpecsService;
    @Override
    public String save(CartonSpecsSDO cartonSpecsSDO) {
        CartonSpecs cartonSpecs = new CartonSpecs();
        BeanUtils.copyProperties(cartonSpecsSDO,cartonSpecs);
        CreateResponse createResponse = cartonSpecsService.create(cartonSpecs);
        return createResponse.getId();
    }

    @Override
    public Long modify(CartonSepcsRequest request) {
        CartonSpecs cartonSpecs = new CartonSpecs();
        cartonSpecs.setId(request.getId().longValue());
        cartonSpecs.setSpecs(request.getSpecs());
        UpdateResponse updateResponse = cartonSpecsService.update(cartonSpecs);
        return updateResponse.getCount().longValue();
    }

    @Override
    public Long delete(CartonSepcsRequest request) {
        DeleteRequest deleteRequest = new DeleteRequest();
        deleteRequest.setId(request.getId().longValue());
        DeleteResponse response = cartonSpecsService.delete(deleteRequest);
        return response.getCount().longValue();
    }

    @Override
    public List<CartonSpecsSDO> queryList(CartonSepcsRequest cartonSepcsRequest) {
        CartonSpecsLoadListRequest vo = new CartonSpecsLoadListRequest();
        vo.setId(cartonSepcsRequest.getId());
        vo.setScItemId(cartonSepcsRequest.getScItemId());
        vo.setOutScItemCode(cartonSepcsRequest.getOutScItemCode());
        vo.setScItemTitle(cartonSepcsRequest.getScItemTitle());
        vo.setSpecs(cartonSepcsRequest.getSpecs());
        vo.setStart(cartonSepcsRequest.getStart());
        vo.setLimit(cartonSepcsRequest.getLimit());


        Result<List<CartonSpecs>> resp = cartonSpecsService.loadList(vo);
        if (resp == null || CollectionUtils.isEmpty(resp.getResult())){
            return new ArrayList<>();
        }
        List<CartonSpecsSDO> specsSDOS = new ArrayList<>();
        resp.getResult().forEach(cartonSpecs -> {
            CartonSpecsSDO specsSDO = new CartonSpecsSDO();
            BeanUtils.copyProperties(cartonSpecs,specsSDO);
            specsSDOS.add(specsSDO);
        });
        return specsSDOS;
    }
}
